# coding=utf-8
from pymongo import MongoClient
from pprint import pprint
import re


def save_motto(quote_item, item_type):
    try:
        item = {}
        quote_content = quote_item.xpath("./text()").extract_first()
        pprint(quote_content)
        if quote_content:
                quote_id_url = quote_item.xpath("./@href").extract_first()
                quote_id = re.search("\d+", quote_id_url).group(0)
                item["id"] = quote_id
                item["content"] = quote_content
                item["type"] = item_type
                # pprint(item)
                save_item(item_dict=item)


    except Exception as ex:
        print(str(ex))

mongo_client = MongoClient("localhost", 27017)


def save_item(item_dict):
    mongo_client["mottos"]["mottos"].insert_one(item_dict)
    pprint(item_dict)

